package com.allen.retrofit2_rxjava2.view.selectimageview.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.view.selectimageview.entity.PhotoInfo;
import com.allen.retrofit2_rxjava2.view.selectimageview.utils.RotateImageViewAware;
import com.allen.retrofit2_rxjava2.view.selectimageview.utils.ThumbnailsUtil;
import com.allen.retrofit2_rxjava2.view.selectimageview.utils.UniversalImageLoadTool;

/**
 * @author 作者： Administrator
 *
 * @version 创建时间：2014年9月1日上午9:41:34
 */
public class PhotoGridAdapter extends BaseAdapter {
	private List<PhotoInfo> list;
	private ViewHolder viewHolder;
	int width;
	private Context context;

	public PhotoGridAdapter(Context context, List<PhotoInfo> list, int width) {
		this.list = list;
		this.context = context;
		this.width = width / 3;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
    @Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PhotoInfo photoInfo = list.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_photo_grid, null);
			ImageView imageView = (ImageView) convertView
					.findViewById(R.id.img_photo);
			viewHolder.img = imageView;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		LayoutParams layoutParams = viewHolder.img
				.getLayoutParams();
		layoutParams.width = width;
		layoutParams.height = width;
		viewHolder.img.setLayoutParams(layoutParams);

		if (photoInfo != null) {
			UniversalImageLoadTool.disPlay(
					ThumbnailsUtil.MapgetHashValue(photoInfo.getImage_id(),
							photoInfo.getPath_file()),
					new RotateImageViewAware(viewHolder.img, photoInfo
							.getPath_absolute()), R.mipmap.defult_head);
		}
		return convertView;
	}

	public class ViewHolder {
		public ImageView img;
	}
}
